<template>
    <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
    >
        <el-form-item label="名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="addr">
            <el-input v-model="ruleForm.addr"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="网址" prop="web">
            <el-input v-model="ruleForm.web"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="note">
            <el-input type="textarea" v-model="ruleForm.note"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
import Axios from 'axios';
export default {
    data() {
        return {
            ruleForm: {
                name: '',
                addr: '',
                phone: '',
                type: [],
                note: '',
                web: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入活动名称', trigger: 'blur' },
                    { min: 3, max: 7, message: '长度在 3 到 7 个字符', trigger: 'blur' }
                ],
                addr: [{ required: true, message: '请填写地址', trigger: 'blur' }],
                phone: [{ required: true, message: '请填写电话', trigger: 'blur' }],
                note: [{ required: true, message: '请填写备注', trigger: 'blur' }],
                web: [{ required: true, message: '请填写网址', trigger: 'blur' }]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(async valid => {
                if (valid) {
                    const supplierId = 123456789;
                    const state = '未审核';
                    const res =await Axios({
                        method: 'post',
                        url: '/api/supplier/supplier',
                        data: {
                            name: this.ruleForm.name,
                            addr: this.ruleForm.addr,
                            phone: this.ruleForm.phone,
                            web: this.ruleForm.web,
                            note: this.ruleForm.note,
                            state: state
                        }
                    });
                    console.log(res)
                   this.$router.push('supplierList')
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
};
</script>

<style>
</style>